package com.cxh.service.impl;

import com.cxh.base.Data1ServiceImpl;
import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.SoundQuDao;
import com.cxh.entity.SoundQu;
import com.cxh.service.SoundQuService;
import com.cxh.util.CommonUtility;
import com.shimne.util.NumberUtil;
import com.shimne.util.ObjectUtil;
import com.shimne.util.StringUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 类说明：
 * @author  ChenXH
 * @date  2019-01-04 16:06:48
 */
@Service("soundQuService")
@DataSource("dataSource1")
public class SoundQuServiceImpl extends Data1ServiceImpl<SoundQu> implements SoundQuService {

	@Autowired
	private SoundQuDao soundQuDao;

	public List<SoundQu> loadAllByOrg(Integer orgId) {
		List<SoundQu> list = new ArrayList<SoundQu>();
		if(ObjectUtil.notNull(orgId)){
			list = soundQuDao.loadAllByOrg(orgId);
		}
		return list;
	}

	public SoundQu findById(Integer id) {
		if(id > 0){
			return soundQuDao.findById(id);
		}
		return null;
	}
	@Transactional(readOnly = false)
	public void update(SoundQu soundQu) {
		if(!ObjectUtil.isNull(soundQu)){
			soundQuDao.update(soundQu);
		}
	}
	
	
	@Transactional(readOnly = false)
	public Integer add(SoundQu soundQu) {
		if(!ObjectUtil.isNull(soundQu)){
			soundQuDao.add(soundQu);
		}
		return soundQu.getId();
	}
	

	//根据单位Id获得单位下形相应的基础问答数据
	public List<Map<String, Object>> loadSoundQuByOrg(Integer orgId,
			int pageNumber, int pageSize) {
		int start=0;
		int max=0;
		if(orgId<=0){
			return null;
		}
		//获取总记录数量
		int count=soundQuDao.findCountByOrg(orgId);
		start = (pageNumber-1)*pageSize;
		if((start+1)>count){
			return null;
		}else{
			max = pageNumber*pageSize-1;
			Map<String,Object> paramaters=new HashMap<String,Object>();
			paramaters.put("start", start);
			paramaters.put("max", max);
			paramaters.put("orgId", orgId);
			
			return soundQuDao.loadSoundQuByOrg(paramaters);
		}
	}

	public int findCountByOrg(Integer orgId) {
		if(orgId<=0){
			return 0;
		}
		return soundQuDao.findCountByOrg(orgId);
	}
	
	public int findCountByUser(Integer userId) {
		if(userId<=0){
			return 0;
		}
		return soundQuDao.findCOuntByUser(userId);
	}

	public List<SoundQu> checkQuest(Map<String, Object> map) {
		if(!ObjectUtil.isNull(map)){
			return soundQuDao.checkQuest(map);
		}
		return null;
	}
	//获得机器人下的问答库
	public List<SoundQu> loadByCode(String code) {
		if(StringUtil.isTrimEmpty(code)){
			return null;
		}
		return soundQuDao.loadByCode(code);
	}

	public List<Map<String, Object>> loadSoundQuByUser(Integer userId,
			int pageNumber, int pageSize) {
		int start=0;
		int max=0;
		if(userId<=0){
			return null;
		}
		//获取总记录数量
		int count=soundQuDao.findCOuntByUserId(userId);
		start = (pageNumber-1)*pageSize;
		if((start+1)>count){
			return null;
		}else{
			max = pageNumber*pageSize-1;
			Map<String,Object> paramaters=new HashMap<String,Object>();
			paramaters.put("start", start);
			paramaters.put("max", max);
			paramaters.put("userId", userId);
			List<Map<String,Object>> list=soundQuDao.loadByUser(paramaters);
			return list;
		}
	}
	//根据用户Id以及基础问答与答案关联获得机器人列表
	public List<Map<String, Object>> loadRobotByUserAndQuestion(Integer userId,
			Integer id) {
		if(userId<=0){
			return null;
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("id", id);
		return soundQuDao.loadRobotByUserAndQuestion(map);
	}
	
	//根据问题Id获得可以回答此问题的机器人Id里诶表
	public List<Integer> findRobotByQuestion(Integer userId,
			Integer id) {
		if(id<=0 || userId<=0){
			return null;
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("questionId", id);
		return soundQuDao.findRobotByQuestion(map);
	}

	//根据用户Id获得用户基础问答列表
	public List<Map<String, Object>> findQuestionsByUser(Integer userId,int page,int pageSize) {
		if(userId<=0){
			return null;
		}
		int start=0;
		if(userId<=0){
			return null;
		}
		//获取总记录数量GuoCL
		//if((start+1)>count){
		page=page==1?0:(page-1)*pageSize;
			Map<String,Object> paramaters=new HashMap<String,Object>();
		paramaters.put("start",page);
		paramaters.put("max", pageSize);
		paramaters.put("userId", userId);
		List<Map<String,Object>> list=soundQuDao.findQuestionsByUser(paramaters);
		return list;
	}
	
	//根据问题与答案关联Id获得问题详情
	public List<Map<String, Object>> findQquestionByAnId(Integer userId) {
		if(userId<=0){
			return null;
		}
		return soundQuDao.findQquestionByAnId(userId);
	}
	

	//根据问题标题获得问题对象
	public SoundQu findQuestionIdByTitle(String title) {
		if(StringUtil.isTrimEmpty(title)){
			return null;
		}
		return soundQuDao.findQuestionIdByTitle(title);
	}

	@Override
	public List<Map<String, Object>> selectListBySynchronize(int countRow) {
		return soundQuDao.selectListBySynchronize(countRow);
	}

	@Override
	public String findSoundQuAnAnswerByOrgId(int orgId, String question) {
		return null;
	}
}
